//导入db数据库
const db = require("../db/mysql")
//获取文章分类函数
exports.getArtCate = (req, res) => {

    //定义sql  查询 is_delete = 0 未删除的文章类别 并按id升序响应给客服端
    let sql = "SELECT * FROM ev_article_cate WHERE is_delete = ? order by id asc"

    //执行sql语句
    db.query(sql, 0, (err, results) => {
        //执行sql语句报错
        if (err) {
            return res.cc(err)
        }
        res.send({
            status: 0,
            message: "文章类别列表查询成功",
            data: results
        })
    })
}


//添加文章类别
exports.addArtCate = (req, res) => {
    //查询分类和别名是否已经存在
    let querySql = "SELECT * FROM ev_article_cate WHERE name = ? or alias = ?"
    db.query(querySql, [req.body.name, req.body.alias], (err, results) => {
        if (err) {
            return res.cc(err)
        }
        console.log(results)
        let cate = req.body;
        //第一种情况 分类和别名 都被占用
        if (results.length === 2) {
            return res.cc("分类名称和别名已被占用")
        }
        if (results.length === 1) {
            if (results[0].name === cate.name && results[0].alias === cate.alias) {
                return res.cc("分类名称和别名已被占用")
            }
            if (results[0].name === cate.name) {
                return res.cc("分类名称已被占用")
            }
            if (results[0].alias === cate.alias) {
                return res.cc("别名已被占用")
            }
        }

        //定义sql
        let sql = "INSERT INTO ev_article_cate SET ?"

        db.query(sql, req.body, (err, result) => {
            if (err) {
                return res.cc(err)
            }
            if (result.affectedRows !== 1) {
                return res.cc("文章分类添加失败")
            }
            res.cc("文章分类添加成功", 0)
        })

    })

}


//删除分类
exports.deleteCate = (req, res) => {

    //定义sql 更新数据状态
    let sql = "UPDATE ev_article_cate SET is_delete = ? WHERE id = ?"
    db.query(sql, [1, req.params.id], (err, results) => {
        if (err) {
            return res.cc(err)
        }
        if (results.affectedRows !== 1) {
            return res.cc("文章类别删除失败")
        }
        res.cc("文章类别删除成功", 0)
    })

}

//根据id获取分类
exports.getCateById = (req, res) => {
    //定义sql语句
    let sql = "SELECT * FROM ev_article_cate WHERE id=?"
    //查询sql语句
    db.query(sql, req.params.id, (err, results) => {
        if (err) {
            return res.cc(err)
        }
        if (results.length !== 1) {
            return res.cc("未查询到该分类")
        }
        res.send({
            status: 0,
            message: "文章分类查询成功",
            data: results[0]
        })
    })
}

//修改类别
exports.updateCate = (req, res) => {

    //查询分类和别名是否已经存在
    let querySql = "SELECT * FROM ev_article_cate WHERE id <> ? and (name = ? or alias = ?)"
    db.query(querySql, [req.body.id, req.body.name, req.body.alias], (err, results) => {
        if (err) {
            return res.cc(err)
        }
        let cate = req.body;
        console.log(results)
        //第一种情况 分类和别名 都被占用
        if (results.length === 2) {
            return res.cc("分类名称和别名已被占用")
        }
        if (results.length === 1) {
            if (results[0].name === cate.name && results[0].alias === cate.alias) {
                return res.cc("分类名称和别名已被占用")
            }
            if (results[0].name === cate.name) {
                return res.cc("分类名称已被占用")
            }
            if (results[0].alias === cate.alias) {
                return res.cc("别名已被占用")
            }
        }

        //定义sql语句
        let sql = "UPDATE ev_article_cate SET ? WHERE id = ?";
        db.query(sql, [req.body, req.body.id], (err, results) => {
            if (err) {
                return res.cc(err)
            }
            if (results.affectedRows !== 1) {
                return res.cc("文章类别更新失败")
            }
            res.cc("文章类别更新成功", 0);
        })
    })

}














